﻿using System.Net;
using Blazor.CoffeeTalesApi;
using Blazor.CoffeeTalesApi.Dto;
using Blazor.CoffeeTalesApi.Entity;
using Blazor.Model.Dto;

namespace Blazor.Services.Update;

public class UpdateService(AppSetting.AppSetting appSetting, ILogger<UpdateService> logger) : IUpdateService
{
    private readonly ApiClient _client = new(logger, appSetting.MainlandApiBaseUri, appSetting.OverseasApiBaseUri);

    public async Task<IPAddress?> FetchMyIpAsync()
        => await _client.FetchMyIpAsync();

    public async Task<ChannelInfo?> FetchChannelInfoAsync(Channel channel)
        => await _client.FetchChannelInfoAsync(channel);

    public async Task<UpdateInfo?> FetchUpdateInfoAsync(Channel channel, uint version, string deviceId)
        => await _client.FetchUpdateInfoAsync(channel, version, deviceId);

    public async Task<bool> SetNativeVersionAsync(Channel channel, uint latestVersion, uint minVersion)
        => await _client.SetNativeVersionAsync(channel, latestVersion, minVersion);

    public async Task<bool> SetJumpUrlAsync(Channel channel, Uri uri)
        => await _client.SetJumpUrlAsync(channel, uri);

    public async Task<bool> AddHotUpdateSettingAsync(Channel channel, uint nativeVersion,
        ResourceVersionEntry resourceVersion, string resourceUrl)
        => await _client.AddHotUpdateSettingAsync(channel, nativeVersion, resourceVersion, resourceUrl);

    public async Task<bool> DeleteHotUpdateSettingAsync(Channel channel, uint nativeVersion)
        => await _client.DeleteHotUpdateSettingAsync(channel, nativeVersion);

    public async Task<bool> SetResourceVersionAsync(Channel channel, uint nativeVersion,
        ResourceVersionEntry resourceVersion)
        => await _client.SetResourceVersionAsync(channel, nativeVersion, resourceVersion);

    public async Task<bool> SetResourceUrlAsync(Channel channel, uint nativeVersion, string url)
        => await _client.SetResourceUrlAsync(channel, nativeVersion, url);

    public async Task<bool> AddWhitelistAsync(Channel channel, uint nativeVersion, IWhitelistKey key,
        ResourceVersionEntry resourceVersion)
        => await _client.AddWhitelistAsync(channel, nativeVersion, key, resourceVersion);

    public async Task<bool> UpdateWhitelistAsync(Channel channel, uint nativeVersion, IWhitelistKey key,
        ResourceVersionEntry resourceVersion)
        => await _client.UpdateWhitelistAsync(channel, nativeVersion, key, resourceVersion);

    public async Task<bool> RemoveWhitelistAsync(Channel channel, uint nativeVersion, IWhitelistKey key)
        => await _client.RemoveWhitelistAsync(channel, nativeVersion, key);

    public async Task<bool> ClearWhitelistAsync(Channel channel, uint nativeVersion)
        => await _client.ClearWhitelistAsync(channel, nativeVersion);

    public async Task<IDictionary<IWhitelistKey, ResourceVersionEntry>?> FetchWhitelistAsync(Channel channel,
        uint nativeVersion)
        => await _client.FetchWhitelistAsync(channel, nativeVersion);
}